import type { App } from 'vue'
import dayjs from 'dayjs'
import useUserStore from '@/store/modules/user'
import { findShopMarketByParam } from '@/api/order'

let siteList: any = []
let user = localStorage.getItem('user')

const getSiteList = async () => {
  const res: any = await findShopMarketByParam()
  siteList = res?.data || []
}

if (user) getSiteList()
/**
 * 时间转换
 * @param {*} time 时间
 * @param {*} type 格式
 */
const setTime = (time: any, type = 'YYYY-MM-DD HH:mm:ss') => {
  return time ? dayjs(time).format(type) : '--'
}
/**
 * 提取对象内容
 * @param {*} obj 提取对象
 * @param {*} path 字段路径
 * @param {*} dftval 默认数据
 */
const extract = (obj: any, path: string, dftval: any) => {
  if (!obj || !path) {
    return ''
  }
  try {
    const paths = path.split('.')
    for (let i = 0; i < paths.length; i++) {
      if (!Object.prototype.hasOwnProperty.call(obj, paths[i])) {
        return ''
      }
      obj = obj[paths[i]]
    }
    return obj || obj == 0 ? obj : dftval ? dftval : ''
  } catch (error) {
    return ''
  }
}
/**
 * 图片转换以及缩略图
 * @param {*} url 图片地址
 * @param {*} w 缩略图宽度
 * @param {*} h 缩略图高度
 */
const setImg = (url: string, w: number, h: number) => {
  if (!url) return ''
  url = url.replace('http://yfn-ftp.oss-cn-shenzhen.aliyuncs.com/', 'https://cdn2.selleroa.com/ftp/')
  const index = url.indexOf('?')
  if (url.indexOf('http://static-erp.oss-cn-shenzhen.aliyuncs.com/') > -1 && index > -1) {
    url = url.substring(0, index)
  }
  url = url.replace('http://static-erp.oss-cn-shenzhen.aliyuncs.com/', 'https://cdn2.selleroa.com/')

  //图片格式是BMP时,加缩略宽高导致图片地址打不开 所以是BMP时不给图片加宽高
  const ext = url.substr(url.lastIndexOf('.') + 1)
  const isBMP = ['BMP'].includes(ext)

  if (w && h && !isBMP) {
    url += url.indexOf('https://cdn2.selleroa.com/') > -1 ? `${url.indexOf('?') > -1 ? '&' : '?'}x-oss-process=image/resize,w_${w}` : `${url.indexOf('?') > -1 ? '&' : '?'}&xprocess=resize&_width=${w}&_height=${h}`
  }
  url = url.replace(':3388/', ':10001/')
  return url
}
/**
 * 拥有权限
 * @param {*} code 权限代码
 * @param {*} filterAdmin 是否过滤管理员  true管理员默认有权限 false管理员走权限代码逻辑
 */

const authPass = (code: string, filterAdmin = true) => {
  const userStore = useUserStore()
  if (!code) {
    return false
  }
  if (userStore.admin && filterAdmin) {
    return true
  }
  const authCodeMap: any = userStore?.authCodeMap || {}
  return authCodeMap[code] ? true : false
}

const getMarketplaceName = (id: string) => {
  let obj = siteList.find((e: any) => e.marketplaceId == id)
  return obj ? obj.marketplaceName : '--'
}

export function setGlobalProperties(app: App): void {
  app.config.globalProperties.$authPass = authPass
  app.config.globalProperties.$setTime = setTime
  app.config.globalProperties.$extract = extract
  app.config.globalProperties.$setImg = setImg
  app.config.globalProperties.$getMarketplaceName = getMarketplaceName
}
declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $authPass: any
    $setTime: any
    $extract: any
    $setImg: any
    $getMarketplaceName: any
  }
}
